<?php

class Tinduc extends Controller {
	
	public $path = 'tinduc/';
	public $langid;
	public $data;
	public $catpro;
	public $option;
	public $catnews;
	public $message;
	public $keyw;
	
	function Tinduc() {
		parent::Controller ();
		
		$this->load->library('cart');
		
		// Config
		$this->config->load('myn');
		$this->config->load( 'build_config' );
		
		// Helper
		$this->load->helper('tinduc_helper');
		$this->load->helper('nonvn_helper');	
		$this->load->helper( 'visited_helper' );
		
		// Model	
		$this->load->model('tinduc_model');
		
		// Language		
		$lg	= $this->uri->segment(1);
		$this->langid = get_lang_value( $lg );
		$this->lang->load( 'l'.$this->langid, 'vietnamese' );
		
		if ( $lg ) {
			$this->session->set_userdata('lang_id', $lg);
		}
		
		
				
		// Product cat
		get_category( $this->langid, 0, 2, $this->catpro);
			
		// Product cat
		get_category( $this->langid, 0, 0, $this->catnews);
		
		// Option
		$this->option = $this->tinduc_model->get_setting( $this->langid );
		
		// New Product
		
		// Data
		$this->data = array(
			'menutop'	=> $this->tinduc_model->get_menu($this->langid, 0),
			'catpros'	=> $this->catpro,
			'catnews'	=> $this->catnews,
			'option'	=> $this->option,
			'profocus'	=> $this->tinduc_model->get_product_status($this->langid, 1, 10),
			'pronews'	=> $this->tinduc_model->get_product_status($this->langid, 0, 10),
			'language'	=> $this->langid,
			'newserv'	=> $this->tinduc_model->get_news_new($this->langid, 1),
			//'advright'	=> $this->tinduc_model->get_list_adv(1),
			'advright'	=> '',
			'advleft'	=> $this->tinduc_model->get_list_adv(3),
			'newfocus'	=> $this->tinduc_model->get_news_new($this->langid, 0, 10, 'focus')
		);	

		// Config
		$this->config->load('myn');
	}
	
	function index() {
		
		$this->data['title'] 		= $this->option['name'];	
		$this->data['keyword'] 		= $this->option['keywords'];	
		$this->data['description'] 	= $this->option['description'];	
		$this->data['cathome'] 		= $this->tinduc_model->get_cat($this->langid, 2, -1);	
		$this->data['newshome'] 	= $this->tinduc_model->get_news_new($this->langid, 0);	
		$this->data['intro'] 		= $this->tinduc_model->get_detail( $this->langid, 0, 'intro' );
		
		$this->load->view ( $this->path.'header', $this->data );
		$this->load->view ( $this->path.'right', $this->data );
		$this->load->view ( $this->path.'left', $this->data );
		$this->load->view ( $this->path.'home', $this->data );
		$this->load->view ( $this->path.'footer', $this->data );
	}
	
	function cat() {
		$name_cat		= $this->uri->segment(3);
		$id				= (int)$this->uri->segment(5);
		$total			= ''; // Tổng số bản ghi
		$page_item 		= (int)$this->config->item('myn_product_item_page'); // Số bản tin trên 1 trang	
		$page_current	= (int)$this->uri->segment(6);
		$cat_name		= $this->uri->segment(4);
		
		$items = $this->tinduc_model->get_items( $this->langid, $id, 'product', $page_item, $page_current, $total );
		
		/**
		 * Phân trang
		 */
		$this->load->library('pagination');
    	$config['base_url'] 	= base_url() . get_lang() . '/' .'/cat/'.$name_cat.'/'.$cat_name.'/'.$id;
		$config['total_rows'] 	= $total;
		$config['per_page']		= $page_item; 
		$config['cur_page']		= $page_current;
		$config['next_link'] 	= $this->lang->line('next_link');
		$config['prev_link'] 	= $this->lang->line('prev_link');
		$config['first_link'] 	= $this->lang->line('first_link');
		$config['last_link'] 	= $this->lang->line('last_link');
		
		$this->pagination->initialize( $config );
		
		$catname = $this->tinduc_model->get_detail( $this->langid, $id, 'catname');
		
		$this->data['title'] 		= $catname['name'];
		$this->data['items'] 		= $items;
		$this->data['paging'] 		= $this->pagination->create_links();
		$this->data['keyword'] 		= $this->option['keywords'];	
		$this->data['description'] 	= $this->option['description'];	
		
		$this->load->view ( $this->path.'header', $this->data );
		$this->load->view ( $this->path.'right', $this->data );
		$this->load->view ( $this->path.'left', $this->data );
		$this->load->view ( $this->path.'product_list', $this->data );
		$this->load->view ( $this->path.'footer', $this->data );
	}
	
	function service() {		
		$id				= (int)$this->uri->segment(5);
		$total			= ''; // Tổng số bản ghi
		$page_item 		= (int)$this->config->item('myn_news_item_page'); // Số bản tin trên 1 trang	
		$page_current	= (int)$this->uri->segment(6);
		$cat_name		= $this->uri->segment(4);
		
		$items = $this->tinduc_model->get_items( $this->langid, $id, 'service', $page_item, $page_current, $total );
		
		/**
		 * Phân trang
		 */
		$this->load->library('pagination');
    	$config['base_url'] 	= base_url(). get_lang() . '/' .'service/cat/'.$cat_name.'/'.$id;
		$config['total_rows'] 	= $total;
		$config['per_page']		= $page_item; 
		$config['cur_page']		= $page_current;
		$config['next_link'] 	= $this->lang->line('next_link');
		$config['prev_link'] 	= $this->lang->line('prev_link');
		$config['first_link'] 	= $this->lang->line('first_link');
		$config['last_link'] 	= $this->lang->line('last_link');
		
		$this->pagination->initialize( $config );
		
		$catname = $this->tinduc_model->get_detail( $this->langid, $id, 'catname');
		
		$this->data['title'] 		= $catname['name'];
		$this->data['items'] 		= $items;
		$this->data['paging'] 		= $this->pagination->create_links();
		$this->data['keyword'] 		= $this->option['keywords'];	
		$this->data['description'] 	= $this->option['description'];	
		
		$this->load->view ( $this->path.'header', $this->data );
		$this->load->view ( $this->path.'right', $this->data );
		$this->load->view ( $this->path.'left', $this->data );
		$this->load->view ( $this->path.'service_list', $this->data );
		$this->load->view ( $this->path.'footer', $this->data );		
	}
	
	function news() {		
		$id				= (int)$this->uri->segment(5);
		$total			= ''; // Tổng số bản ghi
		$page_item 		= (int)$this->config->item('myn_news_item_page'); // Số bản tin trên 1 trang	
		$page_current	= (int)$this->uri->segment(6);
		$name 			= $this->uri->segment(4);
		$items = $this->tinduc_model->get_items( $this->langid, $id, 'news', $page_item, $page_current, $total );
		
		/**
		 * Phân trang
		 */
		$this->load->library('pagination');
    	$config['base_url'] 	= base_url() . get_lang() . '/' .'news/cat/'.$name.'/'.$id;
		$config['total_rows'] 	= $total;
		$config['per_page']		= $page_item; 
		$config['cur_page']		= $page_current;
		$config['next_link'] 	= $this->lang->line('next_link');
		$config['prev_link'] 	= $this->lang->line('prev_link');
		$config['first_link'] 	= $this->lang->line('first_link');
		$config['last_link'] 	= $this->lang->line('last_link');
		
		$this->pagination->initialize( $config );
				
		$this->data['title'] 		= $this->lang->line('news');
		$this->data['items'] 		= $items;
		$this->data['paging'] 		= $this->pagination->create_links();
		$this->data['keyword'] 		= $this->option['keywords'];	
		$this->data['description'] 	= $this->option['description'];	
		
		$this->load->view ( $this->path.'header', $this->data );
		$this->load->view ( $this->path.'right', $this->data );
		$this->load->view ( $this->path.'left', $this->data );
		$this->load->view ( $this->path.'news_list', $this->data );
		$this->load->view ( $this->path.'footer', $this->data );		
	}
	
	function page() {
		$id	= (int)$this->uri->segment(4);
		
		$row = $this->tinduc_model->get_detail( $this->langid, $id, 'page' );

		if ( $row ) {
			$this->data['title'] 		= $row['name'];		
			$this->data['row'] 			= $row;
			$this->data['keyword'] 		= $this->option['keywords'];	
			$this->data['description'] 	= $this->option['description'];	
					
			$this->load->view ( $this->path.'header', $this->data );
			$this->load->view ( $this->path.'right', $this->data );
			$this->load->view ( $this->path.'left', $this->data );
			$this->load->view ( $this->path.'page', $this->data );
			$this->load->view ( $this->path.'footer', $this->data );
			
		}else show_error('404');
	}
	
	function detail() {
		$id	= (int)$this->uri->segment(5);
		$pa = $this->uri->segment( 3 );
		
		switch ( $pa ) {
			case 'product':
					$row 	= $this->tinduc_model->get_detail( $this->langid, $id, 'product' );
					$html 	= 'product_detail';
					$left 	= 'left';
				break;

			case 'addcart':
					$row 	= $this->tinduc_model->get_detail( $this->langid, $id, 'cart' );
					
					if ( $row ) {
						$data 	= array(
			               'id'      => $row['id'],
			               'qty'     => 1,
			               'price'   => $row['vnd'],
			               'name'    => $row['id']
			            );
						
						$this->cart->insert( $data );
						
						echo '<meta http-equiv="refresh" content="0;URL='. link_main( 'cart' ) .'" />';
						exit();
					} 
					
				break;
				
			case 'news':
					$row 	= $this->tinduc_model->get_detail( $this->langid, $id, 'news' );
					$html 	= 'news_detail';
					$left 	= 'left';
				break;
			
			case 'service':	
					$row 	= $this->tinduc_model->get_detail( $this->langid, $id, 'service' );
					$html 	= 'service_detail';
					$left 	= 'left';
				break;

			case 'download':	
					$row 	= $this->tinduc_model->get_detail( $this->langid, $id, 'download' );
					$html 	= 'download_detail';
					$left 	= 'left';
				break;
					
			default:
					show_error('404');
				break;
		}
		
		if ( $row ) {
			
			$this->data['title'] 		= $row['name'];		
			$this->data['row'] 			= $row;
			$this->data['keyword'] 		= isset( $row['seo_keywords'] ) ? $row['seo_keywords'] : $this->option['keywords'];	
			$this->data['description'] 	= isset( $row['seo_description'] ) ? $row['seo_description'] : $this->option['description'];	
			$this->data['others'] 		= $this->tinduc_model->get_other( $this->langid, $row['id'], $row['cat_id'], $pa );	
			
			if ( $pa == 'download' ) {
				get_category( $this->langid, 0, 4, $catdown);
				$this->data['catdown'] = $catdown;				
			}
			
			$this->load->view ( $this->path.'header', $this->data );
			$this->load->view ( $this->path.'right', $this->data );
			$this->load->view ( $this->path.$left, $this->data );			
			$this->load->view ( $this->path.$html, $this->data );
			$this->load->view ( $this->path.'footer', $this->data );
			
		}else show_error('404');
	}
	
	function contact() {
		
		$order = $this->uri->segment( 3, '' );
		
		if ( isset( $_POST['submit'] ) ) {
			
			$this->load->helper( 'email' );
			
			$fullname 	= isset( $_POST['fullname'] ) ? htmlspecialchars( trim( $_POST['fullname'] ) ) : '';
			$email 		= isset( $_POST['email'] ) ? htmlspecialchars( trim( $_POST['email'] ) ) : '';
			$phone 		= isset( $_POST['phone'] ) ? htmlspecialchars( trim( $_POST['phone'] ) ) : '';
			$address 	= isset( $_POST['address'] ) ? htmlspecialchars( trim( $_POST['address'] ) ) : '';
			$subject 	= isset( $_POST['subject'] ) ? htmlspecialchars( trim( $_POST['subject'] ) ) : '';
			$message 	= isset( $_POST['message'] ) ? htmlspecialchars( trim( $_POST['message'] ) ) : '';
			$anti 		= isset( $_POST['code'] ) ? $_POST['code'] : '';
			
			if ( $fullname == '' ) 	$this->message[] = $this->lang->line('msg_fullname');
			if ( $email == '' ) $this->message[] = $this->lang->line('msg_email');
			if ( $subject == '' ) $this->message[] = $this->lang->line('msg_subject');			
			if ( $anti == '' )  $this->message[] = $this->lang->line('msg_anti');
			if ( $message == '' )  $this->message[] = $this->lang->line('msg_message');
			
			if ( $fullname && $email && $subject && $message ) {
				
				if ( !valid_email( $email ) )
					$this->message[] = $this->lang->line('error_email');

				// Kiểm tra xem người dùng có trả lời đúng câu hỏi không
				$checkanti 	= $this->session->userdata('some');					
				if ( $anti != $checkanti ) 
					$this->message[] = $this->lang->line('error_anti_key');

				if ( $this->message == NULL ) {					
					
					$recipient 	= $this->config->item('myn_email');
					$subject  	= $subject;
					$messages	= $this->lang->line('fullname').' : '.$fullname."\n";
					$messages	.= $this->lang->line('address').' : '.$address."\n";
					$messages	.= $this->lang->line('phone').' : '.$phone."\n";
					$messages	.= $this->lang->line('email').' : '.$email."\n";
					$messages	.= $this->lang->line('message').' : '.$message."\n";
					
					send_email( $recipient, $subject, $messages);
					
					$this->message[] = $this->lang->line('msg_suss');
				}
			}
			
		}
		$this->data['title'] 		= $this->lang->line('contact');	
		$this->data['msg'] 			= $this->message;	
		$this->data['order_show'] 	= $order;
		$this->data['keyword'] 		= $this->option['keywords'];	
		$this->data['description'] 	= $this->option['description'];	
		
		$this->load->view ( $this->path.'header', $this->data );
		$this->load->view ( $this->path.'right', $this->data );
		$this->load->view ( $this->path.'left', $this->data );
		$this->load->view ( $this->path.'contact', $this->data );
		$this->load->view ( $this->path.'footer', $this->data );
	}
	
	function download() {
		
		// Product cat
		get_category( $this->langid, 0, 4, $catdown);
		
		$id				= (int)$this->uri->segment(5);
		$total			= ''; // Tổng số bản ghi
		$page_item 		= 20; // Số bản tin trên 1 trang	
		$page_current	= (int)$this->uri->segment(6);
		$cat_name		= $this->uri->segment(4);
		if ( $cat_name == '' )
			$cat_name = 0;
		 
		$items = $this->tinduc_model->get_items( $this->langid, $id, 'download', $page_item, $page_current, $total );
		
		/**
		 * Phân trang
		 */
		$this->load->library('pagination');
    	$config['base_url'] 	= base_url() . get_lang() . '/' . 'download/cat/' . $cat_name. '/' . $id;
		$config['total_rows'] 	= $total;
		$config['per_page']		= $page_item; 
		$config['cur_page']		= $page_current;
		$config['next_link'] 	= $this->lang->line('next_link');
		$config['prev_link'] 	= $this->lang->line('prev_link');
		$config['first_link'] 	= $this->lang->line('first_link');
		$config['last_link'] 	= $this->lang->line('last_link');
		
		$this->pagination->initialize( $config );
		
		$catname = $this->tinduc_model->get_detail( $this->langid, $id, 'catname');
		
		$this->data['title'] 		= 'Download';		
		$this->data['catdown'] 		= $catdown;	
		$this->data['keyword'] 		= $this->option['keywords'];	
		$this->data['description'] 	= $this->option['description'];	
		$this->data['items'] 		= $items;
		$this->data['paging'] 		= $this->pagination->create_links();
		
		$this->load->view ( $this->path.'header', $this->data );
		$this->load->view ( $this->path.'right', $this->data );
		$this->load->view ( $this->path.'left', $this->data );
		$this->load->view ( $this->path.'download', $this->data );
		$this->load->view ( $this->path.'footer', $this->data );
	}
	
	function search() {
		//$name_cat		= $this->uri->segment(2);
		if ( isset( $_POST['sa'] ) )
			$id	= $_POST['q'];
		else
			$id	= $this->uri->segment(3);
		
		$this->keyw		= $id;	
		$total			= ''; // Tổng số bản ghi
		$page_item 		= (int)$this->config->item('myn_product_item_page'); // Số bản tin trên 1 trang	
		$page_current	= (int)$this->uri->segment(4);
		//$cat_name		= $this->uri->segment(3);
		
		$items = $this->tinduc_model->get_items( $this->langid, $id, 'search', $page_item, $page_current, $total );
		
		/**
		 * Phân trang
		 */
		$this->load->library('pagination');
    	$config['base_url'] 	= base_url().'/search/'.$id;
		$config['total_rows'] 	= $total;
		$config['per_page']		= $page_item; 
		$config['cur_page']		= $page_current;
		$config['next_link'] 	= $this->lang->line('next_link');
		$config['prev_link'] 	= $this->lang->line('prev_link');
		$config['first_link'] 	= $this->lang->line('first_link');
		$config['last_link'] 	= $this->lang->line('last_link');
		$config['uri_segment'] 	= 3;
		$this->pagination->initialize( $config );
		
		//$catname = $this->tinduc_model->get_detail( $this->langid, $id, 'catname');
		
		$this->data['title'] 		= 'Tìm kiếm';
		$this->data['items'] 		= $items;
		$this->data['paging'] 		= $this->pagination->create_links();
		$this->data['keyword'] 		= $this->option['keywords'];	
		$this->data['description'] 	= $this->option['description'];	
		
		$this->load->view ( $this->path.'header', $this->data );
		$this->load->view ( $this->path.'right', $this->data );
		$this->load->view ( $this->path.'left', $this->data );
		$this->load->view ( $this->path.'product_search', $this->data );
		$this->load->view ( $this->path.'footer', $this->data );
	}

	function cart() {
		$this->load->helper('form');
		
		
		if ( isset( $_POST['submit'])) {
			$total 	= $_POST['total'];
			$data 	= array();
			for ( $i = 1; $i < $total; $i++ ) {
				$id 	= $_POST[ $i.'rowid' ];
				$qty 	= $_POST[ $i.'qty' ];
				$data[]	= array(
					'rowid'   => $id,
					'qty'     => $qty
				); 
			}
			
			$this->cart->update($data); 
		}
		
		$this->data['keyword'] 		= $this->option['keywords'];	
		$this->data['description'] 	= $this->option['description'];	
		$this->data['title'] 		= 'Giỏ hàng';
		
		$this->load->view ( $this->path.'header', $this->data );
		$this->load->view ( $this->path.'right', $this->data );
		$this->load->view ( $this->path.'left', $this->data );
		$this->load->view ( $this->path.'cart' );
		$this->load->view ( $this->path.'footer', $this->data );
	}
		

	function build() {
		$this->data['keyword'] 		= $this->option['keywords'];	
		$this->data['description'] 	= $this->option['description'];	
		$this->data['title'] 		= 'Xây dựng máy tính';
		
		$this->load->view ( $this->path.'header', $this->data );
		$this->load->view ( $this->path.'right', $this->data );
		$this->load->view ( $this->path.'left', $this->data );
		$this->load->view ( $this->path.'build', $this->data );
		$this->load->view ( $this->path.'footer', $this->data );
	}
}